/*
 * @Author: xgq
 * @Date: 2020-05-14 16:46:33
 * @LastEditTime: 2020-06-09 15:08:09
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tuyibo\servers\models\m_user_favorites.js
 */

const Sequelize = require('sequelize')



module.exports = (sequelize, DataTypes ) =>{

    class userFavorites extends Sequelize.Model{
        
        static async gettopicInner(id){
            var querydata=[]
            let condition = {
                attributes: ['id','topic_id','associated_id','tp_type'],
                raw:true,
                where:{
                    topic_id:id
                }
            }
            querydata = await this.findAll(condition)
            return querydata;
        }

        /**
         * @author: xgq
         * @description: 根据图片id获取收藏数量
         * @param {type} 
         * @return: 
         */        
        static async getCollectCountByid(img_id){
            var num =await this.count({where:{favorite_type:0,img_id}})
            return num;
        }

         // 根据用户id查出收藏过的图片
        static async getImageIdByUid(uid){
            var list=await this.findAll({attributes:['img_id'],where:{user_id:uid,favorite_type:0},raw:true})
            return list;
        }

        /**
         * @author: xgq
         * @description: 删除收藏
         * @param {type} 
         * @return: 
         */        
        static async deleteCollect(pars) {
            let result = await this.destroy({
                'where': {
                    'img_id': pars.image_id,
                    'user_id': pars.uid,
                    'favorite_type': 0
                }
            })
            return result;
        }

        /**
         * @author: xgq
         * @description: 添加收藏
         * @param {type} 
         * @return: 
         */        
        static async pushCollect(pars) {
            let create_time=parseInt(new Date().getTime()/1000);
            let result = await this.create({
                img_id: pars.image_id,
                user_id: pars.uid,
                favorite_type: 0,
                create_time
            })
            return result;
        }


        // 根据用户id查出下载过的图片
        static async getImageIdByUidInOrder(pars) {
            // let { user_id, page, pagesize } = pars
            var [page=1,pagesize=15,user_id, order = null, offset = null, limit = null] = [pars.page,pars.pagesize,pars.user_id, pars.order, pars.offset, pars.limit]
            let sql = "select images.* from images left join user_favorites uf on images.id = uf.img_id where uf.favorite_type = :favorite_type and uf.user_id = :user_id and images.is_enable=0  order by uf.create_time DESC limit :offset,:pagesize"
            let imageList = await sequelize.query(
                sql, //原生 SQL 语句，参数用 ? 
                { replacements: { favorite_type:0,user_id: user_id, offset: offset, pagesize: parseInt(pagesize) }, type: Sequelize.QueryTypes.SELECT } //replacements替换SQL中的参数
            );
            let countSql = "select count(*) as count from images left join user_favorites uf on images.id = uf.img_id where uf.favorite_type = :favorite_type and uf.user_id = :user_id and images.is_enable=0 "
            let count = await sequelize.query(
                countSql, //原生 SQL 语句，参数用 ? 
                { replacements: { favorite_type:0, user_id: user_id }, type: Sequelize.QueryTypes.SELECT } //replacements替换SQL中的参数
            );
            var result = {
                rows: imageList,
                count: count[0].count
            }
            return result;
        }

         
        /**
         * @author: xgq
         * @description: 根据用户id，图片id,获取用户是否收藏图片
         * @param {type} 
         * @return: 
         */        
        static async checkUserCollect(pars) {
            var user_id = pars.uid;
            var img_id = pars.image_id
            var result = await this.findAll({where:{user_id,img_id,favorite_type:0}})
            if(result.length>0){
                return true
            }else{
                return false
            }
        }
    }

    userFavorites.init({
        id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
        img_id:{type:DataTypes.INTEGER},
        user_id:{type:DataTypes.INTEGER},
        favorite_type:{type:DataTypes.INTEGER },
        create_time: { type: DataTypes.INTEGER }
      }, {
        sequelize,
        modelName: 'user_favorites',
        freezeTableName: true,
        timestamps: false
      });

      return userFavorites;

}

